﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Soundelle.Oscillator
{
    public class CalculatorTachometer : BaseDevice
    {
        public CalculatorTachometer()
        {
        }

        private ParameterType[] _AvailableParameters = { 
            ParameterType.Parameter_DC_Offset,
            ParameterType.Parameter_Amplitude,
            ParameterType.Parameter_Averages,
            ParameterType.Parameter_Threshold
        };
        override public ParameterType[] AvailableParameters
        {
            get
            { return _AvailableParameters; }
        }

        private DeviceFunctionType[] _AvailableFunctions = new DeviceFunctionType[] {
             DeviceFunctionType.Function_CalculatorTachometer,
             DeviceFunctionType.Function_Thru
        };
        override public DeviceFunctionType[] AvailableFunctions
        {
            get
            { return _AvailableFunctions; }
        }

        override public void Reset()
        {
            prevTriggerTime = -1.0;
            prevSig = 0.0;

            prevAve = 0.0;
            averageCount = 0;
            base.Reset();
        }

        double prevTriggerTime = -1.0;
        double prevSig = 0.0;

        double prevAve = 0.0;
        long averageCount = 0;

        override public double SignalOut()
        {
            if (!Enabled)
                return 0.0;

            double signalOut = 0.0;
            double signalIn = 0.0;

            double DcOffset = Parameter_DC_Offset;
            double amplitude = Parameter_Amplitude;
            double frequency = 0.0;
            double phase = Parameter_Phase;

            double duty = Parameter_Duty;

            long averages = Parameter_Averages;
            double threshold = Parameter_Threshold;

            DoModulationByChildDevices(ref signalOut, ref signalIn, ref amplitude, ref frequency, ref phase, ref duty);

            //----

            double sig = 0.0;

            if (averages < 1)
                averages = 1;

            switch (Function)
            {
                case DeviceFunctionType.Function_CalculatorTachometer:
                    sig = prevAve;

                    if (prevSig < threshold && signalIn >= threshold) // Triggered
                    {
                        if (prevTriggerTime >= 0.0) // not 1st trigger
                        {
                            sig = amplitude * SamplingRate / (TimePosition - prevTriggerTime);

                            sig = (prevAve * (double)averageCount + sig) / ((double)averageCount + 1.0);
                            if ((averageCount + 1) < averages)
                                ++averageCount;

                            prevAve = sig;
                        }
                        prevTriggerTime = TimePosition;
                    }

                    prevSig = signalIn;
                    break;

                case DeviceFunctionType.Function_Thru:
                    sig = signalIn;
                    break;

                default:
                    break;
            }

            ++TimePosition;
            double SignalOut = sig + signalOut + DcOffset;
            SetPreviousSignalOut(SignalOut);
            return SignalOut;
        }
    }
}
